<?php
/**
 * 数据库操作类
 * @author HuangJie
 * @version 1.0
 * @create date 2008-08-29
 * @update date
 */


class ClubDbManager{
    private $link_id;
    private $host;
    private $user;
    private $password;
    private $debug = false;

    /*构造函数*/
    public function __construct(){
    }

    /*析构函数*/
    public function __destruct(){
        $this->close();
    }

    /**
     * 建立数据库连接
     *
     * @param string $dbname      数据库名或IP地址
     * @param int $port           端口地址
     * @param string $user        用户名
     * @param string $password    密码
     * @return class $ClubDbManager 类本身的引用
     */
    public function connect($dbname,$port,$user,$password){
        $this->host = $dbname.':'.$port;
        $this->user = $user;
        $this->password = $password;
        $this->link_id = mysql_connect($this->host,$this->user,$this->password);


        if ($this->link_id){
            return $this;
        }else{
            return false;
        }
    }

    /**
     *关闭数据库连接
     */
    public function close(){
        if ($this->link_id){
            mysql_close($this->link_id);
        }
    }

    /**
     * 释放数据库结果内存
     *
     * @param resource $query_id
     */
    private function free($query_id){
        mysql_free_result($query_id);
    }

    /**
     * 执行SQL语句
     *
     * @param string $str_sql
     * @return resource $query_id
     */
    private function query($str_sql){
        $str_sql = trim($str_sql);

        if (eregi("^UPDATE",$str_sql) || eregi("^DELETE",$str_sql)){
            if (!eregi(" +WHERE +",$str_sql)){
                $this->halt('语法输入错误 OR 禁止全部删除/更新操作。');
                return false;
            }
        }elseif (eregi("^INSERT",$str_sql) || eregi("^SELECT",$str_sql)){
            //Nothing;
        }else{
            $this->halt("数据库操作核心库异常／SQL基本语法错。请将下述内容反馈给开发人员，以求尽快解决。谢谢！{$str_sql}");
            return false;
        }

        return mysql_query($str_sql,$this->link_id);
    }

    /**
     * 执行写入SQL语句
     *
     * @param string $str_sql
     * @return bool result
     */
    public function executeSql($str_sql){
        $query_id = $this->query($str_sql);

        if ($query_id){
            return true;
        }else{
            $this->halt($str_sql);
            return false;
        }
    }

    /**
     * 执行查询语句，返回查询结果
     *
     * @param string $str_sql
     * @return array $arr_result
     */
    public function selectSql($str_sql){
        $query_id = $this->query($str_sql);
        $arr_result = array();

        if ($query_id){
            while ($row = mysql_fetch_assoc($query_id)) {
                $arr_result[] = $row;
            }
            $this->free($query_id);
            return $arr_result;
        }else{
            $this->halt($str_sql);
            return false;
        }
    }

    /**
     * 取得上一步 INSERT 操作产生的 ID
     *
     * @return int $id;
     */
    public function getInsertId(){
        if ($this->link_id){
            return mysql_insert_id($this->link_id);
        }else{
            return 0;
        }
    }

    public function getAffectedRows(){
    if ($this->link_id){
            return mysql_affected_rows($this->link_id);
        }else{
            return false;
        }
    }

    /**
     * 设置Debug模式
     *
     * @param bool $debug
     */
    public function setDebug($debug){
        $this->debug = $debug;
    }

    /**
     * 输出错误信息，中断程序执行
     *
     * @param string $msg
     */
    private function halt($msg){

        if ($this->debug){
            echo "<!--Database error: {$msg}  \n
            Current page: {$_SERVER['PHP_SELF']} \n
            Error_no: ",mysql_errno($this->link_id),"\n
            Error_msg: ",mysql_error($this->link_id),"\n
            -->";

            exit;
        }
    }
}
?>